<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
  </forms>
</template>

<script>
import s94Mjs from "s94-js";
import {iframe} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";
import {decode_config} from "@/js/s94_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let forms_config = {
      nickname: {label:'用户昵称',name:'nickname'},
      kickback_type_id: {label:'佣金类型',name:'kickback_type_id',type:'radio', config: [],tips:'', value:0},
      money: {label:'佣金金额',name:'money',type:'text_one',config:'number',tips:''},
      title: {label:'佣金标题',name:'title',type:'text_one',tips:'', value:'后台调整'},
      info: {label:'详情',name:'info',type:'text_more',tips:''},
      link_table: {label:'关联数据表',name:'link_table',type:'text_one',tips:''},
      link_id: {label:'关联数据id',name:'link_id',type:'text_one',config:'number',value:0,tips:''},
    }
    if (id){
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
      forms_config.money.is_info =
          forms_config.link_table.is_info =
              forms_config.link_id.is_info = true;
    }else {
      forms_config['nickname'].remove = true;
      forms_config = Object.assign({
        user_id: {label:'指定用户',name:'user_id',type:'list_id',config:'user/index',tips:''}
      }, forms_config)
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,

      kickback_type_map: {},
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/user_kickback/edit?id=`+this.id : '/admin/user_kickback/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('user_kickback/index', '$refs.list_url.list');
          iframe.hide('user_kickback/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })

    }
  },
  watch: {
    'config.kickback_type_id.value'(v){
      let title = this.kickback_type_map[v] || "";
      if (title) this.config.title.value = title+'-调整';
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/user_kickback/info?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('user_kickback/edit');
        }else {
          this.$router.push('index');
        }
      })
    }

    //其他初始化操作
    ajax({url:'/admin/user_kickback/config_map', cache:true}, (res)=>{
      this.kickback_type_map = res.data.kickback_type_map;
      let config = decode_config(res.data.kickback_type_map);
      config.unshift({"content":"无", value:"0"})
      this.config.kickback_type_id.config = config;
    })

  }
}
</script>
